package tjs.study.notes.dotest.proxy.bytebuddy;

import net.bytebuddy.agent.builder.AgentBuilder;
import tjs.study.notes.dotest.proxy.bytebuddy.service.Source;

/**
 * Created by Nicky.Tang on 2021/10/9 11:05 上午
 *
 * @since 02.12.10
 */
public class DoTestOfByteBuddyAgent {

    /**
     * 启动命令：
     * -javaagent:/Users/piangpiang/Documents/my-workspace/my-mayun/tjs-study-notes-test2/tjs-study-notes/tjs-study-notes/tjs-study-dotest/src/main/java/tjs/study/notes/dotest/proxy/bytebuddy/agentConfig/DoTestOfByteBuddyAgent.jar
     *
     * 生成字节码的核心数据流向：
     * xxx.installOn(ins);
     * {@link AgentBuilder.Default.ExecutingTransformer#transform(java.lang.ClassLoader, java.lang.String, java.lang.Class, java.security.ProtectionDomain, byte[])}
     *      ExecutingTransformer extends ClassFileTransformer fixme 底层依然是javaagent
     * {@link AgentBuilder.Default.ExecutingTransformer.LegacyVmDispatcher#run()}
     * {@link net.bytebuddy.agent.builder.AgentBuilder.Default.ExecutingTransformer#doTransform(net.bytebuddy.utility.JavaModule, java.lang.ClassLoader, java.lang.String, java.lang.Class, boolean, java.security.ProtectionDomain, net.bytebuddy.pool.TypePool, net.bytebuddy.dynamic.ClassFileLocator)}
     *      (return dynamicType.getBytes();)
     *
     * 断点条件：
     * name.indexOf("tjs.study.notes.dotest.proxy.bytebuddy.Source")!=-1
     * 将byte数组，转成.class文件：
     * new FileOutputStream(new File("xxxx/tjs/study/notes/dotest/proxy/bytebuddy/Source.class")).write(dynamicType.getBytes());
     *
     * 注意事项：
     * 1.代理的方法必须为静态的，原因可以查看重置后的class文件，其实现就是静态调用
     * 2.agentmain不行，ByteBuddy貌似只能在premain中使用，待验证
     */
    public static void main(String[] args) throws Exception {
        while (true) {
            Source source = new Source();
            source.hellowWorld();
            System.out.println("tostring: "+source.toString());
            System.out.println("");
            Thread.sleep(1000);
        }
    }
}
